import { message } from "antd";
import { getdeleteCheckPending, getDelContentDetails,getDetailesStatisticalData,reviewDeleteContent,getDeletedDetails,getDeletedList } from "../services/services";
export default {
  namespace: "del",

  state: {
    tableData: [],//删除待审核列表
    total: 0,//删除待审核总数
    detail: {},//删除待审核详情
    statistiData: [],//删除待审核统计数据
    deleteTableData:[],//删除待审核详情表格数据
    deletedTableData:[],//已删除列表
    deletedTotal:0//已删除总数
  },
  // 订阅
  subscriptions: {},
  // 同步
  reducers: {
    setTableData(state, action) {
      return { ...state, tableData: action.tableData };
    },
    setTotal(state, action) {
      return { ...state, total: action.total };
    },
    setDetail(state, action) {
      return { ...state, detail: action.detail };
    },
    setStatistiData(state, action) {
      return { ...state, statistiData: action.statistiData };
    },
    setDeleteTableData(state, action) {
      return { ...state, deleteTableData: action.deleteTableData };
    },
    setDeletedTableData(state, action) {
      return { ...state, deletedTableData: action.deletedTableData };
    },
    setDeletedTotal(state, action) {
      return { ...state, deletedTotal: action.deletedTotal };
    },
  },
  // 异步
  effects: {
    /**
     * 列表
     */
    *getdeleteCheckPending({ payload }, { call, put, select }) {
      const res = yield call(getdeleteCheckPending, payload);
      if (res.code === 200) {
        yield put({ type: "setTableData", tableData: res.data.data });
        yield put({ type: "setTotal", total: res.data.total });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 文章详情
     */
    *getDelContentDetails({ payload }, { call, put, select }) {
      const res = yield call(getDelContentDetails, payload);
      if (res.code === 200) {
        yield put({ type: "setDetail", detail: res.data });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 文章详情统计数据
     */
    *getDetailesStatisticalData({ payload }, { call, put, select }) {
      const res = yield call(getDetailesStatisticalData, payload);
      if (res.code === 200) {
        let sdata = [];
        let arr = ["阅读量", "评论量", "点赞量", "分享量"];
        let arr1 = ["scan_count", "comment_count", "like_count", "share_count"];
        for (let i = 0; i < 4; i++) {
          let obj = {};
          obj["x"] = arr[i];
          obj["y"] = res.data[arr1[i]];
          sdata.push(obj);
        }
        yield put({ type: "setStatistiData", statistiData: sdata });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
     /**
     * 审核
     */
    *reviewDeleteContent({ payload }, { call, put, select }) {
      const res = yield call(reviewDeleteContent, payload);
      if (res.code === 200) {
        message.success(res.msg)
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
     /**
     * 详情表格
     */
    *getDeletedDetails({ payload }, { call, put, select }) {
      const res = yield call(getDeletedDetails, payload);
      if (res.code === 200) {
        let tableData = [];
        tableData.push(res.data);
        yield put({ type: "setDeleteTableData",deleteTableData: tableData });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
    /**
     * 已删除列表
     */
    *getDeletedList({ payload }, { call, put, select }) {
      const res = yield call(getDeletedList, payload);
      if (res.code === 200) {
        yield put({ type: "setDeletedTableData", deletedTableData: res.data.data });
        yield put({ type: "setDeletedTotal", deletedTotal: res.data.total });
        return true;
      } else {
        message.error(res.msg);
        return false;
      }
    },
  }
};
